package com.example.fw;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;

public class NavigationHelper extends WebDriverHelperBase {

	public NavigationHelper(ApplicationManager manager) {
		super(manager);
	}

	public void mainPage() {
		if (! onMainPage()){
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
			click(By.linkText("home"));
			}
		return;	
	}	
	
	private boolean onMainPage() {
		driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
		return driver.findElements(By.id("maintable")).size() > 0;
	}
	
	public void printPhonesPage() {
		if (! onPrintPhonesPage()) {	
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
			click(By.linkText("print phones"));
		}
		return;	
	}	

	private boolean onPrintPhonesPage() {
		driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
		if (driver.getCurrentUrl().contains("print&phones")
				&& driver.findElements(By.name("Address book")).size() > 0){
		    return true;
		} else {
			return false;
		}
	}

	public void groupsPage() {
		if (! onGroupsPage()) {
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
			click(By.linkText("groups"));
		}
		return;	
	}
	
	private boolean onGroupsPage() {
		driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
		if (driver.getCurrentUrl().contains("/groups.php")
				&& driver.findElements(By.name("name")).size() > 0) {
		    return true;
		} else {
			return false;
		}
	}

}
